﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;

/// <summary>
/// Summary description for PersonDetails
/// </summary>
[Serializable]
public class Publisher
{
    public int id {get;set;}
	public string publisher {get;set;}
	public string country_id {get;set;}
	public string country {get;set;}
	public string city_id            {get;set;}
	public string city               {get;set;}
	public string address            {get;set;}
	public string postalcode         {get;set;}
	public string email1             {get;set;}
	public string tele1              {get;set;}
	public string tele2              {get;set;}
	public string fax                {get;set;}
	public string website            {get;set;}
	public double longitude          {get;set;}
	public double latitude           {get;set;}

    public void importfromvalues(List<string> fields,System.Collections.ArrayList values)
    {
        int i = 0;
        foreach (string s in fields)
        {
            this.GetType().GetProperty(s).SetValue(this, values[i], null);
            i++;
        }
    }
    public void getojectfrombytes(byte[] bytes)
    {
        System.Runtime.Serialization.Formatters.Binary.BinaryFormatter b = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
        System.IO.MemoryStream mss = new System.IO.MemoryStream(bytes);
        Publisher p  = (Publisher)b.Deserialize(mss);
        System.Reflection.PropertyInfo[] pfarray = p.GetType().GetProperties();
        foreach (System.Reflection.PropertyInfo pf in pfarray)
        { 
          this.GetType().GetProperty(pf.Name).SetValue(this,p.GetType().GetProperty(pf.Name).GetValue(p,null),null);
        }
    
    }
   	public byte[] GetObjectbytes()
	{
	    try
	    {
	        // create new memory stream
	        System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream();
	 
	        // create new BinaryFormatter
	        System.Runtime.Serialization.Formatters.Binary.BinaryFormatter _BinaryFormatter
	                    = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
	 
	        // Serializes an object, or graph of connected objects, to the given stream.
	        _BinaryFormatter.Serialize(_MemoryStream, this);
	         
	        // convert stream to byte array and return
	        return _MemoryStream.ToArray();
	    }
	    catch (Exception _Exception)
	    {
	        // Error
	        Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
	    }
	 
	    // Error occured, return null
	    return null;
	}
    public void getfromdatabase(int id)
    {
        string connString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

        string selectString = "select * from Pendingobjects where id = @id";
        byte[] bytes = null;
        using (SqlConnection sqlConn = new SqlConnection(connString))
        {
            sqlConn.Open();

            using (SqlCommand sqlCmd = new SqlCommand(selectString, sqlConn))
            {
                sqlCmd.Parameters.AddWithValue("@id", id);
                SqlDataReader reader = sqlCmd.ExecuteReader();
                while (reader.Read())
                {
                    bytes = (byte[])reader["data"];
                }
            }
            sqlConn.Close();
        }
        if (bytes != null)
        {
            this.getojectfrombytes(bytes);
        }

    }
    /// <summary>
    /// Set object to Data Base 
    /// </summary>
    /// <param name="objectname">Lik Publisher name or distributer name or company name or ..</param>
    
    public void setobjecttodatabase(string objectname, int companywhichadd, string usernamewhichadd)
    {
        string connString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        string InsertString = "Insert into pendingobjects (type,name,data,companyid,username) values (@type,@name,@data,@companyid,@username)";
        SqlConnection sqlConn = new SqlConnection(connString);
        SqlCommand command = sqlConn.CreateCommand();
        sqlConn.Open();
        command.CommandText = InsertString;
        command.CommandType = System.Data.CommandType.Text;
        command.Parameters.AddWithValue("@type", "publisher");
        command.Parameters.AddWithValue("@name", objectname);
        command.Parameters.AddWithValue("@data", GetObjectbytes());
        command.Parameters.AddWithValue("@companyid", companywhichadd);
        command.Parameters.AddWithValue("@username", usernamewhichadd);
        command.ExecuteNonQuery();
        sqlConn.Close();
    }
	public Publisher()    
	{
		//
		// TODO: Add constructor logic here
		//
	}
}